package com.kikyou.exception;
import com.kikyou.pojo.RespBean;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * Description:全局异常捕捉
 * @author: ykq11  <br/>
 * Date: 2022/3/5:22:51  <br/>
 */
@RestControllerAdvice
/**
  *表示控制器的增强类，如果发生异常，并且符合类中 自定义的拦截异常类的时候就会被拦截
  **/
public class GlobalException {

    @ExceptionHandler(SQLException.class)
    public RespBean mySqlException(SQLException e) {
        if (e instanceof SQLIntegrityConstraintViolationException) {
            return RespBean.error("该数据有关联数据，操作失败");
        }
        return RespBean.error("数据库异常，操作失败！");
    }

    @ExceptionHandler(AuthenticationException.class)
    public RespBean noUser(AuthenticationException e) {
        if (e instanceof AuthenticationException) {
            return RespBean.error("用户名不存在");
        }
        return RespBean.error("用户名密码不正确");
    }
}
